package fr.upmc.i3s.upmcontact.backend;

import java.io.File;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.GetDataCallback;
import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.parse.SignUpCallback;

import fr.upmc.i3s.upmcontact.Constants;
import fr.upmc.i3s.upmcontact.R;
import fr.upmc.i3s.upmcontact.callbacks.IGetDataCallback;
import fr.upmc.i3s.upmcontact.callbacks.IPostDataCallback;
import fr.upmc.i3s.upmcontact.callbacks.ISignInCallback;
import fr.upmc.i3s.upmcontact.callbacks.IUpdateDataCallback;
import fr.upmc.i3s.upmcontact.contents.ChatMessage;
import fr.upmc.i3s.upmcontact.contents.Incident;
import fr.upmc.i3s.upmcontact.contents.LostFoundObject;
import fr.upmc.i3s.upmcontact.contents.MerchantInfo;
import fr.upmc.i3s.upmcontact.contents.StudentLifeInfo;
import fr.upmc.i3s.upmcontact.contents.UpmcInfo;
import fr.upmc.i3s.upmcontact.contents.User;
import fr.upmc.i3s.upmcontact.tools.BitmapFactoryAdapter;
import fr.upmc.i3s.upmcontact.tools.MobileDataUtils;
import fr.upmc.i3s.upmcontact.tools.PreferencesManager;
import fr.upmc.i3s.upmcontact.views.FragmentController;
import fr.upmc.i3s.upmcontact.views.connection.SignInActivity;
import fr.upmc.i3s.upmcontact.views.dialogs.NoConnectivityDialog;

public class BackEndBridgeImpl implements BackEndBridge {
	
	private static final String TAG = "BackEndParseImpl";
	
	  ///////////////////////
	 // BACKEND CONSTANTS //
	///////////////////////
	
	public static final String APPLICATION_ID = "lken7qlOOU6DnGkQddYiIIVTTGBVGt8cHsTh0mN6";
	public static final String CLIENT_KEY = "A90KxoUaxq66BUcjiydKljfTBtB0cgoT0DPbEjy4";
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////
	
	private static final int LOADING_DIALOG_BUTTON = 0;
	private static final int NO_CONNECTIVITY_DIALOG = 1;
	private static final int LOADING_DIALOG = 2;
	
	private static final int CANCEL_FOR_A_WHILE = 0;
	
	  ///////////////
	 // SINGLETON //
	///////////////
	
	private Activity context;
	private final LinkedList<Dialog> dialogs;
	private final LinkedList<Boolean> values;
	
	private static volatile BackEndBridgeImpl INSTANCE;
	public static BackEndBridgeImpl getInstance(Activity context) {
        if (BackEndBridgeImpl.INSTANCE == null) {
            synchronized(BackEndBridgeImpl.class) {
              if (BackEndBridgeImpl.INSTANCE == null) {
            	  BackEndBridgeImpl.INSTANCE = new BackEndBridgeImpl(context);
         }	}	}
         BackEndBridgeImpl.INSTANCE.context = context;
         return BackEndBridgeImpl.INSTANCE;
	}
	
	public BackEndBridgeImpl(Activity context) {
		this.context = context;
		this.values = new LinkedList<Boolean>();
		this.values.add(/*cancelForAWhile=*/false);
		this.dialogs = new LinkedList<Dialog>();
		this.dialogs.add(createProgressDialogButton());
		AlertDialog ncd = NoConnectivityDialog.build(context);
		this.dialogs.add(ncd);
		this.dialogs.add(createProgressDialog());
	}
	
	@Override
	public void reinitBackEnd(Activity context) {
		this.context = context;
		values.clear();
		this.values.add(/*cancelForAWhile=*/false);
		dialogs.clear();
		this.dialogs.add(createProgressDialogButton());
		AlertDialog ncd = NoConnectivityDialog.build(context);
		this.dialogs.add(ncd);
		this.dialogs.add(createProgressDialog());
	}
	
	private ProgressDialog createProgressDialog() {
		ProgressDialog pd = new ProgressDialog(context);
		pd.setIndeterminate(true);
		pd.setCancelable(false);
		return pd;
	}
	
	private ProgressDialog createProgressDialogButton() {
		ProgressDialog pd = new ProgressDialog(context);
		pd.setIndeterminate(true);
		pd.setCancelable(false);
		pd.setButton(DialogInterface.BUTTON_NEUTRAL, context.getString(R.string.dialog_background), new Dialog.OnClickListener(){
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				Toast.makeText(context, R.string.dialog_background, Toast.LENGTH_SHORT).show();
				setCancelForAWhile(true);
			}
		});
		return pd;
	}
	
	private void cancelProgressDialog(boolean cancelDialog) {
		if(cancelDialog) { 
			dialogs.get(LOADING_DIALOG_BUTTON).cancel();
			if(FragmentController.getInstance() != null) {
				FragmentController.getInstance().setLoadingOff();
			}
		}
	}
	
	@Override
	public void setCancelForAWhile(boolean value) {
		this.values.remove(CANCEL_FOR_A_WHILE);
		this.values.add(value);
	}
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////
	
	  /////////////
	 // SIGN UP //
	/////////////

	@Override
	public void signup(User user) {
		Log.d(TAG, "signup.user="+user);
		if(!MobileDataUtils.isNetworkAvailable(context)) {
			dialogs.get(NO_CONNECTIVITY_DIALOG).show();
		} else {
			dialogs.get(LOADING_DIALOG);
			dialogs.get(LOADING_DIALOG).setTitle(context.getString(R.string.action_sign_up_short));
			((ProgressDialog) dialogs.get(LOADING_DIALOG)).setMessage(context.getString(R.string.verifying_account));
			dialogs.get(LOADING_DIALOG).show();
			final Bundle extras = new Bundle();
			extras.putString("username", user.getUsername());
			ParseUser parseUser = new ParseUser();
			parseUser.setUsername(user.getUsername());
			parseUser.setPassword(user.getPassword());
			parseUser.setEmail(user.getEmail());
			parseUser.signUpInBackground(new SignUpCallback() {
				public void done(ParseException e) {
					Log.d(TAG, "signUpInBackground.SignUpCallback");
					if (e == null) {
						Toast.makeText(context, R.string.success_sign_up, Toast.LENGTH_SHORT).show();
						dialogs.get(LOADING_DIALOG).cancel();
						Intent intent = new Intent(context, SignInActivity.class);
						intent.putExtras(extras);
						context.startActivity(intent);
						context.finish();
					} else {
						Log.e(TAG, "ParseException : "+e);
						Toast.makeText(context, context.getString(R.string.error_parse_signup)+" : "+e.getMessage(), Toast.LENGTH_SHORT).show();
						dialogs.get(LOADING_DIALOG).cancel();
					}
				}
			});
		}
	}
	
	
	  /////////////
	 // SIGN IN //
	/////////////

	@Override
	public void signin(ISignInCallback callback) {
		User user = PreferencesManager.getSignInSettings(context);
		signin(user, callback);
	}
	
	@Override
	public void signin(User user, final ISignInCallback callback) {
		Log.d(TAG, "signin.user="+user);
		if(!MobileDataUtils.isNetworkAvailable(context)) {
			dialogs.get(NO_CONNECTIVITY_DIALOG).show();
			if(callback != null) { callback.signed(false); }
		} else {
			dialogs.get(LOADING_DIALOG).setTitle(context.getString(R.string.action_sign_in_short));
			((ProgressDialog) dialogs.get(LOADING_DIALOG)).setMessage(context.getString(R.string.verifying_account));
			dialogs.get(LOADING_DIALOG).show();
			ParseUser.logInInBackground(user.getUsername(), user.getPassword(), new LogInCallback() {
				@Override
				public void done(ParseUser user, ParseException e) {
					Log.d(TAG, "logInInBackground.LogInCallback.user="+user);
					if (user != null) {
						if(callback != null) { callback.signed(true); }
						dialogs.get(LOADING_DIALOG).cancel();
					} else {
						Log.e(TAG, "ParseException : "+e);
						Toast.makeText(context, context.getString(R.string.error_parse_signin)+" : "+e.getMessage(), Toast.LENGTH_SHORT).show();
						if(callback != null) { callback.signed(false); }
						dialogs.get(LOADING_DIALOG).cancel();
					}
				}
			});
		}
	}
	
	
	  /////////////
	 // LOG OUT //
	/////////////

	@Override
	public void logout() {
		Log.d(TAG, "logout");
		ParseUser.logOut();
		PreferencesManager.clearSignInSettings(context);
		Intent intent = new Intent(context, SignInActivity.class);
		context.startActivity(intent);
		context.finish();
	}
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////
	
	  //////////
	 // USER //
	//////////
	
	@Override
	public User getCurrentUser() {
		Log.d(TAG, "getCurrentUser");
		ParseUser pu = ParseUser.getCurrentUser();
		if(pu != null) {
			User user = new User();
			user.setUsername(pu.getUsername());
			user.setEmail(pu.getEmail());
			user.setKeepLogged(PreferencesManager.isKeepLogged(context.getApplicationContext()));
			return user;
		}
		return null;
	}
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////
	
	  ///////////////////////
	 // OBJECTS CONSTANTS //
	///////////////////////
	
	// ALL OBJECTS
	public static final String PARSE_CREATED_AT = "createdAt";
	public static final String PARSE_UPDATED_AT = "updatedAt";
	
	// INCIDENTS
	public static final int PARSE_INCIDENT_STATUS_SIGNALED = 0;
	public static final int PARSE_INCIDENT_STATUS_ENDED = 1;
	public static final String PARSE_INCIDENT = "IncidentModel";
	public static final String PARSE_INCIDENT_PLACE = "place";
	public static final String PARSE_INCIDENT_TYPE = "type";
	public static final String PARSE_INCIDENT_PRIORITY = "priority";
	public static final String PARSE_INCIDENT_DESCRIPTION = "description";
	public static final String PARSE_INCIDENT_SIGNALED_BY = "signaledBy";
	public static final String PARSE_INCIDENT_STATUS = "status";
	public static final String PARSE_INCIDENT_PHOTO = "photo";
	
	// LOST AND FOUND OBJECTS
	public static final int PARSE_OBJECT_LOST_VALUE = 0;
	public static final int PARSE_OBJECT_FOUND_VALUE = 1;
	public static final int PARSE_OBJECT_STATUS_SIGNALED = 0;
	public static final int PARSE_OBJECT_STATUS_ENDED = 1;
	public static final String PARSE_OBJECT = "LostFoundModel";
	public static final String PARSE_OBJECT_NAME = "name";
	public static final String PARSE_OBJECT_DESCRIPTION = "description";
	public static final String PARSE_OBJECT_PLACE = "place";
	public static final String PARSE_OBJECT_PHOTO = "photo";
	public static final String PARSE_OBJECT_SIGNALED_BY = "signaledBy";
	public static final String PARSE_OBJECT_TYPE = "type";
	public static final String PARSE_OBJECT_STATUS = "status";

	// UPMC INFOS 
	public static final String PARSE_UPMC_INFO = "InformationModel";
	public static final String PARSE_UPMC_INFO_TITLE = "title";
	public static final String PARSE_UPMC_INFO_DESCRIPTION = "description";
	public static final String PARSE_UPMC_INFO_PHOTO = "photo";
	public static final String PARSE_UPMC_INFO_LINK = "link";
	public static final String PARSE_UPMC_INFO_PUBLISHED_BY = "publishedBy";
	public static final String PARSE_UPMC_INFO_RECIPIENTS = "recipients";
	
	// STUDENT LIFE INFOS
	public static final String PARSE_STUDENT_INFO = "BDEModel";
	public static final String PARSE_STUDENT_INFO_TITLE = "title";
	public static final String PARSE_STUDENT_INFO_DESCRIPTION = "description";
	public static final String PARSE_STUDENT_INFO_PHOTO = "photo";
	public static final String PARSE_STUDENT_INFO_LINK = "link";
	public static final String PARSE_STUDENT_INFO_PUBLISHED_BY = "publishedBy";
	public static final String PARSE_STUDENT_INFO_EVENT_TYPE = "eventType";
	public static final String PARSE_STUDENT_INFO_ADDITIONAL_INFOS = "additionalInfos";
	
	// MERCHANTS INFOS
	public static final String PARSE_MERCHANT_INFO = "PromotionModel";
	public static final String PARSE_MERCHANT_INFO_TITLE = "title";
	public static final String PARSE_MERCHANT_INFO_DESCRIPTION = "description";
	public static final String PARSE_MERCHANT_INFO_PHOTO = "photo";
	public static final String PARSE_MERCHANT_INFO_LINK = "link";
	public static final String PARSE_MERCHANT_INFO_PUBLISHED_BY = "publishedBy";
	public static final String PARSE_MERCHANT_INFO_BEGIN_DATE = "beginDate";
	public static final String PARSE_MERCHANT_INFO_END_DATE = "endDate";
	public static final String PARSE_MERCHANT_INFO_CONDITIONS = "conditions";
	public static final String PARSE_MERCHANT_INFO_ADDRESS = "address";
	
	// CHAT MESSAGE
	public static final String PARSE_CHAT_MESSAGE = "MessageModel";
	public static final String PARSE_CHAT_MESSAGE_CONTENT = "content";
	public static final String PARSE_CHAT_MESSAGE_FROM = "from";

	///////////////////////////////////////////////////////////////////////////////////////////////////////

	  //////////////////
	 // POST OBJECTS //
	//////////////////
	
	// PARSE GLOBAL //////////////////////// 
	
	private void postParseObject(ParseObject parseObject, final IPostDataCallback callback) {
		Log.d(TAG, "postParseObject");
		if(!MobileDataUtils.isNetworkAvailable(context)) {
			dialogs.get(NO_CONNECTIVITY_DIALOG).show();
			if(callback != null) { callback.posted(false); }
		} else {
			parseObject.saveInBackground(new SaveCallback() {
				@Override
				public void done(ParseException e) {
					Log.d(TAG, "postParseObject.saveInBackground.SaveCallback");
					if(FragmentController.getInstance() != null) {
						FragmentController.getInstance().setLoadingOff();
					}
					dialogs.get(LOADING_DIALOG_BUTTON).cancel();
					if(e == null) {
						Toast.makeText(context, R.string.submit_ended_ok, Toast.LENGTH_SHORT).show();
						if(callback != null) { callback.posted(true); }
					} else { 
						Log.e(TAG, "ParseException : "+e);
						Toast.makeText(context, R.string.submit_ended_nok, Toast.LENGTH_SHORT).show();
						if(callback != null) { callback.posted(false); }
	}	}	});		}	}
	
	
	private void addPhotoAndPostParseObject(final ParseObject parseObject, String photoPath, final String parseRowId, final IPostDataCallback callback) {
		Log.d(TAG, "addPhotoAndPostParseObject");
		if(!MobileDataUtils.isNetworkAvailable(context)) {
			dialogs.get(NO_CONNECTIVITY_DIALOG).show();
			if(callback != null) { callback.posted(false); }
		} else {
			String photoName = new File(photoPath).getName();
			byte[] photoBytes = BitmapFactoryAdapter.getByteArrayFromFile(photoPath, 400, 80);
			final ParseFile photoFile = new ParseFile(photoName, photoBytes);
			photoFile.saveInBackground(new SaveCallback() {
				public void done(ParseException e) {
					Log.d(TAG, "saveInBackground.SaveCallback");
					if(e == null) {
						parseObject.put(parseRowId, photoFile);
						postParseObject(parseObject, callback);
					} else {
						Log.e(TAG, "ParseException : ",e);
						if(callback!=null) { callback.posted(false); }
		}	}	});	}	}
	
	// INCIDENT ////////////////////////
	
	@Override
	public void postIncident(Incident incident) {
		postIncident(incident, null);
	}
	
	@Override
	public void postIncident(Incident incident, final IPostDataCallback callback) {
		postIncident(incident, callback, true);
	}
	
	public void postIncident(Incident incident, final IPostDataCallback callback, boolean displayDialog) {
		Log.d(TAG, "postIncident.incident="+incident);
		if(!MobileDataUtils.isNetworkAvailable(context)) {
			dialogs.get(NO_CONNECTIVITY_DIALOG).show();
			if(callback != null) { callback.posted(false); }
		} else {
			if(FragmentController.getInstance() != null) {
				FragmentController.getInstance().setLoadingOn();
			}
			if(displayDialog && !values.get(CANCEL_FOR_A_WHILE)) {
				dialogs.get(LOADING_DIALOG_BUTTON).setTitle(context.getString(R.string.submit));
				((ProgressDialog) dialogs.get(LOADING_DIALOG_BUTTON)).setMessage(context.getString(R.string.submitting_incident));
				dialogs.get(LOADING_DIALOG_BUTTON).show();
			}
			final ParseObject incidentObject = new ParseObject(PARSE_INCIDENT);
			incidentObject.put(PARSE_INCIDENT_PLACE, incident.getPlace());
			incidentObject.put(PARSE_INCIDENT_TYPE, incident.getType());
			incidentObject.put(PARSE_INCIDENT_PRIORITY, incident.getPriority());
			incidentObject.put(PARSE_INCIDENT_DESCRIPTION, incident.getDescription());
			incidentObject.put(PARSE_INCIDENT_SIGNALED_BY, ParseUser.getCurrentUser());
			incidentObject.put(PARSE_INCIDENT_STATUS, incident.getStatus());
			if(incident.getPhotoPath() != null) {
				addPhotoAndPostParseObject(incidentObject, incident.getPhotoPath(), PARSE_INCIDENT_PHOTO, callback);
			} else {
				postParseObject(incidentObject, callback);
			}
		}
	}
	
	// LOST OBJECT ////////////////////////
	
	@Override
	public void postLostFoundObject(LostFoundObject lfo) {
		postLostFoundObject(lfo, null);
	}
	
	@Override
	public void postLostFoundObject(LostFoundObject lfo, final IPostDataCallback callback) {
		postLostFoundObject(lfo, callback, true);
	}
	
	public void postLostFoundObject(LostFoundObject lfo, final IPostDataCallback callback, boolean displayDialog) {
		Log.d(TAG, "postLostFoundObject.lfo="+lfo);
		if(!MobileDataUtils.isNetworkAvailable(context)) {
			dialogs.get(NO_CONNECTIVITY_DIALOG).show();
			if(callback != null) { callback.posted(false); }
		} else {
			if(FragmentController.getInstance() != null) {
				FragmentController.getInstance().setLoadingOn();
			}
			if(displayDialog && !values.get(CANCEL_FOR_A_WHILE)) {
				dialogs.get(LOADING_DIALOG_BUTTON).setTitle(context.getString(R.string.submit));
				((ProgressDialog) dialogs.get(LOADING_DIALOG_BUTTON)).setMessage(context.getString(R.string.submitting_lost_object));
				dialogs.get(LOADING_DIALOG_BUTTON).show();
			}
			final ParseObject lfObject = new ParseObject(PARSE_OBJECT);
			lfObject.put(PARSE_OBJECT_NAME, lfo.getName());
			lfObject.put(PARSE_OBJECT_DESCRIPTION, lfo.getDescription());
			lfObject.put(PARSE_OBJECT_PLACE, lfo.getPlace());
			lfObject.put(PARSE_OBJECT_TYPE, lfo.getType());
			lfObject.put(PARSE_OBJECT_STATUS, lfo.getStatus());
			lfObject.put(PARSE_OBJECT_SIGNALED_BY, ParseUser.getCurrentUser());
			if(lfo.getPhotoPath() != null) {
				addPhotoAndPostParseObject(lfObject, lfo.getPhotoPath(), PARSE_OBJECT_PHOTO, callback);
			} else {
				postParseObject(lfObject, callback);
			}
		}
	}
	
	// CHAT MESSAGE ////////////////////////
	
	@Override
	public void postChatMessage(ChatMessage message) {
		postChatMessage(message, null);
	}
	
	@Override
	public void postChatMessage(ChatMessage message, IPostDataCallback callback) {
		postChatMessage(message, callback, true);
	}
	
	public void postChatMessage(ChatMessage message, IPostDataCallback callback, boolean displayDialog) {
		Log.d(TAG, "postChatMessage.cm="+message);
		if(!MobileDataUtils.isNetworkAvailable(context)) {
			dialogs.get(NO_CONNECTIVITY_DIALOG).show();
			if(callback != null) { callback.posted(false); }
		} else {
			if(FragmentController.getInstance() != null) {
				FragmentController.getInstance().setLoadingOn();
			}
			if(displayDialog && !values.get(CANCEL_FOR_A_WHILE)) {
				dialogs.get(LOADING_DIALOG_BUTTON).setTitle(context.getString(R.string.submit));
				((ProgressDialog) dialogs.get(LOADING_DIALOG_BUTTON)).setMessage(context.getString(R.string.submitting_chat_message));
				dialogs.get(LOADING_DIALOG_BUTTON).show();
			}
			final ParseObject cmObject = new ParseObject(PARSE_CHAT_MESSAGE);
			cmObject.put(PARSE_CHAT_MESSAGE_CONTENT, message.getMessageContent());
			cmObject.put(PARSE_CHAT_MESSAGE_FROM, ParseUser.getCurrentUser());
			postParseObject(cmObject, callback);
		}
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////

	  /////////////////
	 // GET OBJECTS //
	/////////////////
	
	// LOST OBJECTS ////////////////////////
	
	@Override
	public void getLostObjects(final IGetDataCallback callback) {
		getLostObjects(callback, true, true);
	}
	
	public void getLostObjects(final IGetDataCallback callback, boolean displayDialog, final boolean cancelDialog) {
		Log.d(TAG, "getLostObjects.cancelDialog="+cancelDialog);
		if(!MobileDataUtils.isNetworkAvailable(context)) {
			dialogs.get(NO_CONNECTIVITY_DIALOG).show();
			if(callback != null) { callback.getDataEndedNok(); }
		} else {
			if(FragmentController.getInstance() != null) {
				FragmentController.getInstance().setLoadingOn();
			}
			if(displayDialog && !values.get(CANCEL_FOR_A_WHILE)) {
				dialogs.get(LOADING_DIALOG_BUTTON).setTitle(context.getString(R.string.get));
				((ProgressDialog) dialogs.get(LOADING_DIALOG_BUTTON)).setMessage(context.getString(R.string.get_lost_objects));
				dialogs.get(LOADING_DIALOG_BUTTON).show();
			}
			final LinkedList<Object> list = new LinkedList<Object>();
			ParseQuery<ParseObject> query = ParseQuery.getQuery(BackEndBridgeImpl.PARSE_OBJECT);
			query.whereEqualTo(PARSE_OBJECT_TYPE, PARSE_OBJECT_LOST_VALUE);
			query.whereEqualTo(PARSE_OBJECT_STATUS, PARSE_OBJECT_STATUS_SIGNALED);
			query.orderByDescending(PARSE_CREATED_AT);
			query.addDescendingOrder(PARSE_CREATED_AT);
			query.findInBackground(new FindCallback<ParseObject>() {
				public void done(final List<ParseObject> objects, ParseException e) {
					if(e == null) {
						Log.i(TAG, "getLostObjects.list.size="+objects.size());
						if(objects.size() == 0) {
							if(callback != null) { callback.getDataEndedOk(list); }
							cancelProgressDialog(cancelDialog);
						} else {
							for(final ParseObject po : objects) {
								final LostFoundObject lfo = new LostFoundObject();
								lfo.setId(po.getObjectId());
								lfo.setName(po.getString(PARSE_OBJECT_NAME));
								lfo.setDescription(po.getString(PARSE_OBJECT_DESCRIPTION));
								lfo.setPlace(po.getString(PARSE_OBJECT_PLACE));
								lfo.setStatus(po.getInt(PARSE_OBJECT_STATUS));
								lfo.setType(po.getInt(PARSE_OBJECT_TYPE));
								lfo.setCreatedAt(po.getCreatedAt());
								lfo.setUpdatedAt(po.getUpdatedAt());
								ParseUser signaledBy = (ParseUser) po.get(PARSE_OBJECT_SIGNALED_BY);
								// get "signaledBy" parse user :
								signaledBy.fetchIfNeededInBackground(new GetCallback<ParseUser>() {
									@Override
									public void done(ParseUser user, ParseException e) {
										if(e == null) {
											String id = user.getObjectId();
											Date createdAt = user.getCreatedAt();
											Date updatedAt = user.getUpdatedAt();
											String username = user.getUsername();
											String email = user.getEmail();
											lfo.setSignaledBy(new User(id, createdAt, updatedAt, username, null, email, false));
											// get "photo" if needed :
											ParseFile parseFile = po.getParseFile(PARSE_OBJECT_PHOTO);
											if(parseFile != null) {
												final String photoFileName = parseFile.getName();
												parseFile.getDataInBackground(new GetDataCallback() {
													@Override
													public void done(byte[] data, ParseException e) {
														Log.d(TAG, "getDataInBackground.GetDataCallback.data.length="+data.length);
														if(e == null) {
															String filePath = Constants.LOST_EXTERNAL_FOLDER;
															String photoPath = BitmapFactoryAdapter.saveByteArrayOnExternalStorage(data, filePath, photoFileName);
															lfo.setPhotoPath(photoPath);
														} else {
															Log.e(TAG, "ParseException : ",e);
														}
														list.add(lfo);
														if(list.size()==objects.size()) {
															if(callback != null) { callback.getDataEndedOk(list); }
															cancelProgressDialog(cancelDialog);
												}	}	});
											} else {
												list.add(lfo);
												if(list.size()==objects.size()) {
													if(callback != null) { callback.getDataEndedOk(list); }
													cancelProgressDialog(cancelDialog);
											}	}
										} else {
											Log.e(TAG, "ParseException : "+e);
											list.add(lfo);
											if(list.size()==objects.size()) {
												if(callback != null) { callback.getDataEndedOk(list); }
												cancelProgressDialog(cancelDialog);
						}	}	}	});	}	}
						if(cancelDialog) {
							Toast.makeText(context, R.string.get_ended_ok, Toast.LENGTH_SHORT).show();
							cancelProgressDialog(cancelDialog);
						}
					} else { 
						Log.e(TAG, "ParseException : "+e);
						Toast.makeText(context, R.string.get_ended_nok, Toast.LENGTH_SHORT).show();
						if(callback != null) { callback.getDataEndedNok(); }
						cancelProgressDialog(cancelDialog);
	}	}	});	}	}
	
	
	// FOUND OBJECTS ////////////////////////
	
	@Override
	public void getFoundObjects(final IGetDataCallback callback) {
		getFoundObjects(callback, true, true);
	}
	
	public void getFoundObjects(final IGetDataCallback callback, boolean displayDialog, final boolean cancelDialog) {
		if(!MobileDataUtils.isNetworkAvailable(context)) {
			dialogs.get(NO_CONNECTIVITY_DIALOG).show();
			if(callback != null) { callback.getDataEndedNok(); }
		} else {
			if(FragmentController.getInstance() != null) {
				FragmentController.getInstance().setLoadingOn();
			}
			if(displayDialog && !values.get(CANCEL_FOR_A_WHILE)) {
				dialogs.get(LOADING_DIALOG_BUTTON).setTitle(context.getString(R.string.get));
				((ProgressDialog) dialogs.get(LOADING_DIALOG_BUTTON)).setMessage(context.getString(R.string.get_found_objects));
				dialogs.get(LOADING_DIALOG_BUTTON).show();
			}
			final LinkedList<Object> list = new LinkedList<Object>();
			ParseQuery<ParseObject> query = ParseQuery.getQuery(BackEndBridgeImpl.PARSE_OBJECT);
			query.whereEqualTo(PARSE_OBJECT_TYPE, PARSE_OBJECT_FOUND_VALUE);
			query.whereEqualTo(PARSE_OBJECT_STATUS, PARSE_OBJECT_STATUS_SIGNALED);
			query.orderByDescending(PARSE_CREATED_AT);
			query.addDescendingOrder(PARSE_CREATED_AT);
			query.findInBackground(new FindCallback<ParseObject>() {
				public void done(final List<ParseObject> objects, ParseException e) {
					if(e == null) {
						Log.i(TAG, "getFoundObjects.list.size="+objects.size());
						if(objects.size() == 0) {
							if(callback != null) { callback.getDataEndedOk(list); }
							cancelProgressDialog(cancelDialog);
						} else {
							for(final ParseObject po : objects) {
								final LostFoundObject lfo = new LostFoundObject();
								lfo.setId(po.getObjectId());
								lfo.setName(po.getString(PARSE_OBJECT_NAME));
								lfo.setDescription(po.getString(PARSE_OBJECT_DESCRIPTION));
								lfo.setPlace(po.getString(PARSE_OBJECT_PLACE));
								lfo.setStatus(po.getInt(PARSE_OBJECT_STATUS));
								lfo.setType(po.getInt(PARSE_OBJECT_TYPE));
								lfo.setCreatedAt(po.getCreatedAt());
								lfo.setUpdatedAt(po.getUpdatedAt());
								ParseUser signaledBy = (ParseUser) po.get(PARSE_OBJECT_SIGNALED_BY);
								// get "signaledBy" parse user :
								signaledBy.fetchIfNeededInBackground(new GetCallback<ParseUser>() {
									@Override
									public void done(ParseUser user, ParseException e) {
										if(e == null) {
											String id = user.getObjectId();
											Date createdAt = user.getCreatedAt();
											Date updatedAt = user.getUpdatedAt();
											String username = user.getUsername();
											String email = user.getEmail();
											lfo.setSignaledBy(new User(id, createdAt, updatedAt, username, null, email, false));
											// get "photo" if needed :
											ParseFile parseFile = po.getParseFile(PARSE_OBJECT_PHOTO);
											if(parseFile != null) {
												final String photoFileName = parseFile.getName();
												parseFile.getDataInBackground(new GetDataCallback() {
													@Override
													public void done(byte[] data, ParseException e) {
														Log.d(TAG, "getDataInBackground.GetDataCallback.data="+data);
														if(e == null && data != null) {
															String filePath = Constants.FOUND_EXTERNAL_FOLDER;
															String photoPath = BitmapFactoryAdapter.saveByteArrayOnExternalStorage(data, filePath, photoFileName);
															lfo.setPhotoPath(photoPath);
														} else {
															Log.e(TAG, "ParseException : ",e);
														}
														list.add(lfo);
														if(list.size()==objects.size()) {
															if(callback != null) { callback.getDataEndedOk(list); }
															cancelProgressDialog(cancelDialog);
												}	}	});
											} else {
												list.add(lfo);
												if(list.size()==objects.size()) {
													if(callback != null) { callback.getDataEndedOk(list); }
													cancelProgressDialog(cancelDialog);
											}	}
										} else {
											Log.e(TAG, "ParseException : "+e);
											list.add(lfo);
											if(list.size()==objects.size()) {
												if(callback != null) { callback.getDataEndedOk(list); }
												cancelProgressDialog(cancelDialog);
						}	}	}	});	}	}
						if(cancelDialog) {
							Toast.makeText(context, R.string.get_ended_ok, Toast.LENGTH_SHORT).show();
							cancelProgressDialog(cancelDialog);
						}
					} else { 
						Log.e(TAG, "ParseException : "+e);
						Toast.makeText(context, R.string.get_ended_nok, Toast.LENGTH_SHORT).show();
						if(callback != null) { callback.getDataEndedNok(); }
						cancelProgressDialog(cancelDialog);
	}	}	});	}	}
	
	
	// STUDENT LIFE INFOS ////////////////////////
	
	@Override
	public void getStudentLifeInfos(final IGetDataCallback callback) {
		getStudentLifeInfos(callback, true, true);
	}
	
	public void getStudentLifeInfos(final IGetDataCallback callback, boolean displayDialog, final boolean cancelDialog) {
		if(!MobileDataUtils.isNetworkAvailable(context)) {
			dialogs.get(NO_CONNECTIVITY_DIALOG).show();
			if(callback != null) { callback.getDataEndedNok(); }
		} else {
			if(FragmentController.getInstance() != null) {
				FragmentController.getInstance().setLoadingOn();
			}
			if(displayDialog && !values.get(CANCEL_FOR_A_WHILE)) {
				dialogs.get(LOADING_DIALOG_BUTTON).setTitle(context.getString(R.string.get));
				((ProgressDialog) dialogs.get(LOADING_DIALOG_BUTTON)).setMessage(context.getString(R.string.get_student_life_infos));
				dialogs.get(LOADING_DIALOG_BUTTON).show();
			}
			final LinkedList<Object> list = new LinkedList<Object>();
			ParseQuery<ParseObject> query = ParseQuery.getQuery(BackEndBridgeImpl.PARSE_STUDENT_INFO);
			query.orderByDescending(PARSE_CREATED_AT);
			query.addDescendingOrder(PARSE_CREATED_AT);
			query.findInBackground(new FindCallback<ParseObject>() {
				public void done(final List<ParseObject> objects, ParseException e) {
					if(e == null) {
						Log.i(TAG, "getStudentLifeInfos.list.size="+objects.size());
						if(objects.size() == 0) {
							if(callback != null) { callback.getDataEndedOk(list); }
							cancelProgressDialog(cancelDialog);
						} else {
							for(final ParseObject po : objects) {
								final StudentLifeInfo sli = new StudentLifeInfo();
								sli.setId(po.getObjectId());
								sli.setTitle(po.getString(PARSE_STUDENT_INFO_TITLE));
								sli.setDescription(po.getString(PARSE_STUDENT_INFO_DESCRIPTION));
								sli.setLink(po.getString(PARSE_STUDENT_INFO_LINK));
								sli.setEventType(po.getString(PARSE_STUDENT_INFO_EVENT_TYPE));
								sli.setAdditionalInformations(po.getString(PARSE_STUDENT_INFO_ADDITIONAL_INFOS));
								sli.setCreatedAt(po.getCreatedAt());
								sli.setUpdatedAt(po.getUpdatedAt());
								ParseUser publishedBy = (ParseUser) po.get(PARSE_STUDENT_INFO_PUBLISHED_BY);
								// get "publishedBy" parse user :
								publishedBy.fetchIfNeededInBackground(new GetCallback<ParseUser>() {
									@Override
									public void done(ParseUser user, ParseException e) {
										if(e == null) {
											String id = user.getObjectId();
											Date createdAt = user.getCreatedAt();
											Date updatedAt = user.getUpdatedAt();
											String username = user.getUsername();
											String email = user.getEmail();
											sli.setPublishedBy(new User(id, createdAt, updatedAt, username, null, email, false));
											// get "photo" if needed :
											ParseFile parseFile = po.getParseFile(PARSE_STUDENT_INFO_PHOTO);
											if(parseFile != null) {
												final String photoFileName = parseFile.getName();
												parseFile.getDataInBackground(new GetDataCallback() {
													@Override
													public void done(byte[] data, ParseException e) {
														Log.d(TAG, "getDataInBackground.GetDataCallback.data="+data);
														if(e == null && data != null) {
															String filePath = Constants.UPMC_EXTERNAL_FOLDER;
															String photoPath = BitmapFactoryAdapter.saveByteArrayOnExternalStorage(data, filePath, photoFileName);
															sli.setPhotoPath(photoPath);
														} else {
															Log.e(TAG, "ParseException : ",e);
														}
														list.add(sli);
														if(list.size()==objects.size()) {
															if(callback != null) { callback.getDataEndedOk(list); }
															cancelProgressDialog(cancelDialog);
												}	}	});
											} else {
												list.add(sli);
												if(list.size()==objects.size()) {
													if(callback != null) { callback.getDataEndedOk(list); }
													cancelProgressDialog(cancelDialog);
											}	}
										} else {
											Log.e(TAG, "ParseException : "+e);
											list.add(sli);
											if(list.size()==objects.size()) {
												if(callback != null) { callback.getDataEndedOk(list); }
												cancelProgressDialog(cancelDialog);
						}	}	}	});	}	}
						if(cancelDialog) {
							Toast.makeText(context, R.string.get_ended_ok, Toast.LENGTH_SHORT).show();
							cancelProgressDialog(cancelDialog);
						}
					} else { 
						Log.e(TAG, "ParseException : "+e);
						Toast.makeText(context, R.string.get_ended_nok, Toast.LENGTH_SHORT).show();
						if(callback != null) { callback.getDataEndedNok(); }
						cancelProgressDialog(cancelDialog);
	}	}	});	}	}
	
	
	// UPMC INFOS ////////////////////////
	
	@Override
	public void getUpmcInfos(final IGetDataCallback callback) {
		getUpmcInfos(callback, true, true);
	}
	
	public void getUpmcInfos(final IGetDataCallback callback, boolean displayDialog, final boolean cancelDialog) {
		if(!MobileDataUtils.isNetworkAvailable(context)) {
			dialogs.get(NO_CONNECTIVITY_DIALOG).show();
			if(callback != null) { callback.getDataEndedNok(); }
		} else {
			if(FragmentController.getInstance() != null) {
				FragmentController.getInstance().setLoadingOn();
			}
			if(displayDialog && !values.get(CANCEL_FOR_A_WHILE)) {
				dialogs.get(LOADING_DIALOG_BUTTON).setTitle(context.getString(R.string.get));
				((ProgressDialog) dialogs.get(LOADING_DIALOG_BUTTON)).setMessage(context.getString(R.string.get_upmc_infos));
				dialogs.get(LOADING_DIALOG_BUTTON).show();
			}
			final LinkedList<Object> list = new LinkedList<Object>();
			ParseQuery<ParseObject> query = ParseQuery.getQuery(BackEndBridgeImpl.PARSE_UPMC_INFO);
			query.orderByDescending(PARSE_CREATED_AT);
			query.addDescendingOrder(PARSE_CREATED_AT);
			query.findInBackground(new FindCallback<ParseObject>() {
				public void done(final List<ParseObject> objects, ParseException e) {
					if(e == null) {
						Log.i(TAG, "getUpmcInfos.list.size="+objects.size());
						if(objects.size() == 0) {
							if(callback != null) { callback.getDataEndedOk(list); }
							cancelProgressDialog(cancelDialog);
						} else {
							for(final ParseObject po : objects) {
								final UpmcInfo ui = new UpmcInfo();
								ui.setId(po.getObjectId());
								ui.setTitle(po.getString(PARSE_UPMC_INFO_TITLE));
								ui.setDescription(po.getString(PARSE_UPMC_INFO_DESCRIPTION));
								ui.setLink(po.getString(PARSE_UPMC_INFO_LINK));
								ui.setRecipients(po.getString(PARSE_UPMC_INFO_RECIPIENTS));
								ui.setCreatedAt(po.getCreatedAt());
								ui.setUpdatedAt(po.getUpdatedAt());
								ParseUser publishedBy = (ParseUser) po.get(PARSE_UPMC_INFO_PUBLISHED_BY);
								// get "publishedBy" parse user :
								publishedBy.fetchIfNeededInBackground(new GetCallback<ParseUser>() {
									@Override
									public void done(ParseUser user, ParseException e) {
										if(e == null) {
											String id = user.getObjectId();
											Date createdAt = user.getCreatedAt();
											Date updatedAt = user.getUpdatedAt();
											String username = user.getUsername();
											String email = user.getEmail();
											ui.setPublishedBy(new User(id, createdAt, updatedAt, username, null, email, false));
											// get "photo" if needed :
											ParseFile parseFile = po.getParseFile(PARSE_UPMC_INFO_PHOTO);
											if(parseFile != null) {
												final String photoFileName = parseFile.getName();
												parseFile.getDataInBackground(new GetDataCallback() {
													@Override
													public void done(byte[] data, ParseException e) {
														Log.d(TAG, "getDataInBackground.GetDataCallback.data="+data);
														if(e == null && data != null) {
															String filePath = Constants.UPMC_EXTERNAL_FOLDER;
															String photoPath = BitmapFactoryAdapter.saveByteArrayOnExternalStorage(data, filePath, photoFileName);
															ui.setPhotoPath(photoPath);
														} else {
															Log.e(TAG, "ParseException : ",e);
														}
														list.add(ui);
														if(list.size()==objects.size()) {
															if(callback != null) { callback.getDataEndedOk(list); }
															cancelProgressDialog(cancelDialog);
												}	}	});
											} else {
												list.add(ui);
												if(list.size()==objects.size()) {
													if(callback != null) { callback.getDataEndedOk(list); }
													cancelProgressDialog(cancelDialog);
											}	}
										} else {
											Log.e(TAG, "ParseException : "+e);
											list.add(ui);
											if(list.size()==objects.size()) {
												if(callback != null) { callback.getDataEndedOk(list); }
												cancelProgressDialog(cancelDialog);
						}	}	}	});	}	}
						if(cancelDialog) {
							Toast.makeText(context, R.string.get_ended_ok, Toast.LENGTH_SHORT).show();
							cancelProgressDialog(cancelDialog);
						}
					} else { 
						Log.e(TAG, "ParseException : "+e);
						Toast.makeText(context, R.string.get_ended_nok, Toast.LENGTH_SHORT).show();
						if(callback != null) { callback.getDataEndedNok(); }
						cancelProgressDialog(cancelDialog);
	}	}	});	}	}
		
	
	// MERCHANTS INFOS ////////////////////////
	
	@Override
	public void getMerchantInfos(final IGetDataCallback callback) {
		getMerchantInfos(callback, true, true);
	}
	
	public void getMerchantInfos(final IGetDataCallback callback, boolean displayDialog, final boolean cancelDialog) {
		if(!MobileDataUtils.isNetworkAvailable(context)) {
			dialogs.get(NO_CONNECTIVITY_DIALOG).show();
			if(callback != null) { callback.getDataEndedNok(); }
		} else {
			if(FragmentController.getInstance() != null) {
				FragmentController.getInstance().setLoadingOn();
			}
			if(displayDialog && !values.get(CANCEL_FOR_A_WHILE)) {
				dialogs.get(LOADING_DIALOG_BUTTON).setTitle(context.getString(R.string.get));
				((ProgressDialog) dialogs.get(LOADING_DIALOG_BUTTON)).setMessage(context.getString(R.string.get_merchants_infos));
				dialogs.get(LOADING_DIALOG_BUTTON).show();
			}
			final LinkedList<Object> list = new LinkedList<Object>();
			ParseQuery<ParseObject> query = ParseQuery.getQuery(BackEndBridgeImpl.PARSE_MERCHANT_INFO);
			query.orderByDescending(PARSE_CREATED_AT);
			query.addDescendingOrder(PARSE_CREATED_AT);
			query.findInBackground(new FindCallback<ParseObject>() {
				public void done(final List<ParseObject> objects, ParseException e) {
					if(e == null) {
						Log.i(TAG, "getMerchantInfos.list.size="+objects.size());
						if(objects.size() == 0) {
							if(callback != null) { callback.getDataEndedOk(list); }
							cancelProgressDialog(cancelDialog);
						} else {
							for(final ParseObject po : objects) {
								final MerchantInfo mi = new MerchantInfo();
								mi.setId(po.getObjectId());
								mi.setTitle(po.getString(PARSE_MERCHANT_INFO_TITLE));
								mi.setDescription(po.getString(PARSE_MERCHANT_INFO_DESCRIPTION));
								mi.setLink(po.getString(PARSE_MERCHANT_INFO_LINK));
								mi.setBeginDate(po.getDate(PARSE_MERCHANT_INFO_BEGIN_DATE));
								mi.setEndDate(po.getDate(PARSE_MERCHANT_INFO_END_DATE));
								mi.setConditions(po.getString(PARSE_MERCHANT_INFO_CONDITIONS));
								mi.setMerchantAddress(po.getString(PARSE_MERCHANT_INFO_ADDRESS));
								mi.setCreatedAt(po.getCreatedAt());
								mi.setUpdatedAt(po.getUpdatedAt());
								ParseUser publishedBy = (ParseUser) po.get(PARSE_MERCHANT_INFO_PUBLISHED_BY);
								// get "publishedBy" parse user :
								publishedBy.fetchIfNeededInBackground(new GetCallback<ParseUser>() {
									@Override
									public void done(ParseUser user, ParseException e) {
										if(e == null) {
											String id = user.getObjectId();
											Date createdAt = user.getCreatedAt();
											Date updatedAt = user.getUpdatedAt();
											String username = user.getUsername();
											String email = user.getEmail();
											mi.setPublishedBy(new User(id, createdAt, updatedAt, username, null, email, false));
											// get "photo" if needed :
											ParseFile parseFile = po.getParseFile(PARSE_MERCHANT_INFO_PHOTO);
											if(parseFile != null) {
												final String photoFileName = parseFile.getName();
												parseFile.getDataInBackground(new GetDataCallback() {
													@Override
													public void done(byte[] data, ParseException e) {
														Log.d(TAG, "getDataInBackground.GetDataCallback.data="+data);
														if(e == null && data != null) {
															String filePath = Constants.MERCHANTS_EXTERNAL_FOLDER;
															String photoPath = BitmapFactoryAdapter.saveByteArrayOnExternalStorage(data, filePath, photoFileName);
															mi.setPhotoPath(photoPath);
														} else {
															Log.e(TAG, "ParseException : ",e);
														}
														list.add(mi);
														if(list.size()==objects.size()) {
															if(callback != null) { callback.getDataEndedOk(list); }
															cancelProgressDialog(cancelDialog);
												}	}	});
											} else {
												list.add(mi);
												if(list.size()==objects.size()) {
													if(callback != null) { callback.getDataEndedOk(list); }
													cancelProgressDialog(cancelDialog);
											}	}
										} else {
											Log.e(TAG, "ParseException : "+e);
											list.add(mi);
											if(list.size()==objects.size()) {
												if(callback != null) { callback.getDataEndedOk(list); }
												cancelProgressDialog(cancelDialog);
						}	}	}	});	}	}
						if(cancelDialog) {
							Toast.makeText(context, R.string.get_ended_ok, Toast.LENGTH_SHORT).show();
							cancelProgressDialog(cancelDialog);
						}
					} else { 
						Log.e(TAG, "ParseException : "+e);
						Toast.makeText(context, R.string.get_ended_nok, Toast.LENGTH_SHORT).show();
						if(callback != null) { callback.getDataEndedNok(); }
						cancelProgressDialog(cancelDialog);
	}	}	});	}	}
	
	// CHAT MESSAGES //////////////////////
	
	@Override
	public void getChatMessages(final IGetDataCallback callback) {
		getChatMessages(callback, true, true);
	}
	
	public void getChatMessages(final IGetDataCallback callback, boolean displayDialog, final boolean cancelDialog) {
		if(!MobileDataUtils.isNetworkAvailable(context)) {
			dialogs.get(NO_CONNECTIVITY_DIALOG).show();
			if(callback != null) { callback.getDataEndedNok(); }
		} else {
			if(FragmentController.getInstance() != null) {
				FragmentController.getInstance().setLoadingOn();
			}
			if(displayDialog && !values.get(CANCEL_FOR_A_WHILE)) {
				dialogs.get(LOADING_DIALOG_BUTTON).setTitle(context.getString(R.string.get));
				((ProgressDialog) dialogs.get(LOADING_DIALOG_BUTTON)).setMessage(context.getString(R.string.get_chat_messages));
				dialogs.get(LOADING_DIALOG_BUTTON).show();
			}
			final LinkedList<Object> list = new LinkedList<Object>();
			ParseQuery<ParseObject> query = ParseQuery.getQuery(BackEndBridgeImpl.PARSE_CHAT_MESSAGE);
			query.setLimit(15);
			query.orderByDescending(PARSE_CREATED_AT);
			query.addDescendingOrder(PARSE_CREATED_AT);
			query.findInBackground(new FindCallback<ParseObject>() {
				public void done(final List<ParseObject> objects, ParseException e) {
					if(e == null) {
						Log.i(TAG, "getChatMessages.list.size="+objects.size());
						if(objects.size() == 0) {
							if(callback != null) { callback.getDataEndedOk(list); }
							cancelProgressDialog(cancelDialog);
						} else {
							for(final ParseObject po : objects) {
								final ChatMessage cm = new ChatMessage();
								cm.setId(po.getObjectId());
								cm.setMessageContent(po.getString(PARSE_CHAT_MESSAGE_CONTENT));
								cm.setCreatedAt(po.getCreatedAt());
								cm.setUpdatedAt(po.getUpdatedAt());
								ParseUser from = (ParseUser) po.get(PARSE_CHAT_MESSAGE_FROM);
								// get "from" parse user :
								from.fetchIfNeededInBackground(new GetCallback<ParseUser>() {
									@Override
									public void done(ParseUser user, ParseException e) {
										if(e == null) {
											String id = user.getObjectId();
											Date createdAt = user.getCreatedAt();
											Date updatedAt = user.getUpdatedAt();
											String username = user.getUsername();
											String email = user.getEmail();
											cm.setFrom(new User(id, createdAt, updatedAt, username, null, email, false));
											list.add(cm);
											if(list.size()==objects.size()) {
												if(callback != null) { callback.getDataEndedOk(list); }
												cancelProgressDialog(cancelDialog);
											}
										} else {
											Log.e(TAG, "ParseException : "+e);
											list.add(cm);
											if(list.size()==objects.size()) {
												if(callback != null) { callback.getDataEndedOk(list); }
												cancelProgressDialog(cancelDialog);
						}	}	}	});	}	}
						if(cancelDialog) {
							Toast.makeText(context, R.string.get_ended_ok, Toast.LENGTH_SHORT).show();
							cancelProgressDialog(cancelDialog);
						}
					} else { 
						Log.e(TAG, "ParseException : "+e);
						Toast.makeText(context, R.string.get_ended_nok, Toast.LENGTH_SHORT).show();
						if(callback != null) { callback.getDataEndedNok(); }
						cancelProgressDialog(cancelDialog);
	}	}	});	}	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////

	  ////////////////////
	 // UPDATE OBJECTS //
	////////////////////
	
	// LOST OBJECTS ////////////////////////

	@Override
	public void updateLostFoundObject(final IUpdateDataCallback callback, final LostFoundObject lfo, int status) {
		if(!MobileDataUtils.isNetworkAvailable(context)) {
			dialogs.get(NO_CONNECTIVITY_DIALOG).show();
			if(callback != null) { callback.updated(lfo, false); }
		} else {
			if(FragmentController.getInstance() != null) {
				FragmentController.getInstance().setLoadingOn();
			}
			dialogs.get(LOADING_DIALOG_BUTTON).setTitle(context.getString(R.string.update));
			((ProgressDialog) dialogs.get(LOADING_DIALOG_BUTTON)).setMessage(context.getString(R.string.update_object));
			dialogs.get(LOADING_DIALOG_BUTTON).show();
			ParseQuery<ParseObject> query = ParseQuery.getQuery(PARSE_OBJECT);
			query.getInBackground(lfo.getId(), new GetCallback<ParseObject>() {
				public void done(ParseObject lfObject, ParseException e) {
					if (e == null) {
						lfObject.put(PARSE_OBJECT_STATUS, PARSE_OBJECT_STATUS_ENDED);
						lfObject.saveInBackground();
						Toast.makeText(context, R.string.update_ended_ok, Toast.LENGTH_SHORT).show();
						callback.updated(lfo, true);
					} else {
						Toast.makeText(context, R.string.update_ended_nok, Toast.LENGTH_SHORT).show();
						callback.updated(lfo, false);
					}
					dialogs.get(LOADING_DIALOG_BUTTON).cancel();
	}	});	}	}

}
		